import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import chartsRouter from './modules/charts'
import clipboardRouter from './modules/clipboard'
import componentsRouter from './modules/components'
import errorRouter from './modules/error'
import errorLogRouter from './modules/errorLog'
import exampleRouter from './modules/example'
import excelRouter from './modules/excel'
import nestedRouter from './modules/nested'
import otherRouter from './modules/other'
import pdfRouter from './modules/pdf'
import permissionRouter from './modules/permission'
import tableRouter from './modules/table'
import themeRouter from './modules/theme'
import zipRouter from './modules/zip'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/sdf',
    component: Layout,
    meta: { title: '组件', icon: 'component', affix: true },
    children: [
      {
        path: '/icon/index',
        component: () => import('@/examples/icons/index'),
        name: 'Icons',
        meta: { title: '字体图标', icon: 'icon', noCache: true }
      },
      {
        path: '/tabindex',
        component: () => import('@/examples/tab/index'),
        name: 'Tab',
        meta: { title: '标签页', icon: 'tab' }
      },
      {
        path: '/pdf/download',
        component: () => import('@/examples/pdf/download'),
        // name: 'pdfDownload',
        // meta: { title: '下载', icon: 'tab' },
        hidden: true
      },
      {
        path: 'external-link',
        component: { render: (e) => e('router-view') },
        children: [
          {
            path: 'https:www.baidu.com',
            meta: { title: '外部连接', icon: 'link' }
          }
        ]
      },
      componentsRouter,
      chartsRouter,
      tableRouter,
      exampleRouter,
      excelRouter,
      zipRouter,
      pdfRouter,
      nestedRouter,
      clipboardRouter,
      permissionRouter,
      errorRouter,
      errorLogRouter,
      themeRouter,
      ...otherRouter

    ]
  }
]

export const asyncRoutes = [
  {
    path: '/system',
    name: 'system',
    component: () => import ('@/layout'),
    // redirect:'/dashboard',
    meta: { title: '系统设置', icon: 'el-icon-setting' },
    children: [
      {
        path: '/system/user',
        name: 'sysUser',
        component: () => import ('@/views/system/user'),
        meta: { title: '用户管理', icon: 'el-icon-user' }
      },
      {
        path: '/system/role',
        name: 'sysRole',
        component: () => import ('@/views/system/role'),
        meta: { title: '角色管理', icon: 'peoples' }
      },
      {
        path: '/system/resource',
        name: 'sysResource',
        component: () => import ('@/views/system/resource'),
        meta: { title: '资源管理', icon: 'password' }
      }
    ]
  },
  {
    path: '/service',
    name: 'service',
    component: () => import ('@/layout'),
    // redirect:'/dashboard',
    meta: { title: '服务管理', icon: 'el-icon-camera-solid' },
    children: [
      {
        path: '/service/device',
        name: 'device',
        component: () => import ('@/views/service/device'),
        meta: { title: '设备管理', icon: 'el-icon-camera-solid' }
      },
      {
        path: '/service/server',
        name: 'server',
        component: () => import ('@/views/service/server'),
        meta: { title: '服务器管理', icon: 'nested' }
      }
    ]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
